/**
 * Created by Anard on 2017/4/17.
 */
let md5 = require('md5');
let publicDao = require('./publicDao.js');
let gameData = require('../util/gameData.js');
let tableName = 'user';

module.exports = userDao;

function userDao() {
    this.data = {};
    this.change = [];
    this._levelUpReward = [];
}

userDao.prototype.checkPass = function (password) {
    if (this.data.password != md5(password)) {
        return false;
    }
    return true;
};

userDao.prototype.one = function (err, res, cb) {
    let _this = this;
    if (err !== null) {
        cb({code: err.code, msg: err.message}, null);
    } else if (res.length == 0) {
        cb(null, null);
    } else {
        _this.data = res[0];
        cb(null, {res: res[0]});
    }
};

userDao.prototype.inId = function (id, cb) {
    //let publicModel = new publicDao();
    let _this = this;
    publicDao.init().table(tableName).wheres('id', '=', id).get(function (err, res) {
        _this.one(err, res, cb);
    });
};

userDao.prototype.inAccount = function (account, cb) {
    //let publicModel = new publicDao();
    let _this = this;
    publicDao.init().table(tableName).wheres('account', 'LIKE', account).get(function (err, res) {
        _this.one(err, res, cb);
    });
};

userDao.prototype.inToken = function (token, cb) {
    //let publicModel = new publicDao();
    let _this = this;
    publicDao.init().table(tableName).wheres('token', 'LIKE', token).get(function (err, res) {
        _this.one(err, res, cb);
    });
};

userDao.prototype.inBluedId = function (token, cb) {
    //let publicModel = new publicDao();
    let _this = this;
    var account = token
    publicDao.init().table(tableName).wheres('account', 'LIKE', account).get(function (err, res) {
        _this.one(err, res, cb);
    });
};

userDao.prototype.inLoveLiveId = function (token, cb) { 
    let _this = this; 
    var account = token
    publicDao.init().table(tableName).wheres('account', 'LIKE', account).get(function (err, res) {
        _this.one(err, res, cb);
    });
};

userDao.prototype.inWeixinId = function (token, cb) { 
    let _this = this;
    publicDao.init().table(tableName).wheres('weixinId', 'LIKE', token).get(function (err, res) {
        _this.one(err, res, cb);
    });
};

userDao.prototype.set = function (key, value) {
    this.data[key] = value;
    this.change.push(key);

    return this;
};

userDao.prototype.add = function (key, value) {
    this.data[key] += parseInt(value);
    if (key == 'score' || key == 'gold') {
        if (this.data[key] < 0) {
            this.data[key] = 0;
        }
    }
    this.change.push(key);

    return this;
};

userDao.prototype.addExp = function (exp) {
    this.data.exp += parseInt(exp);
    this.change.push('exp');
    this.levelUp();

    return this;
};

userDao.prototype.levelUp = function () {
    let lv = this.data.lv;
    if (lv < 1) {
        lv = 1;
    }
    while (true) {
        if (gameData.d.level[lv - 1].exp > this.data.exp) {
            break;
        }
        lv++;
        this._levelUpReward.push(gameData.d.level[lv - 1].reward);
    }
    if (lv != this.data.lv) {
        this.set('lv', lv);
    }
};

userDao.prototype.isLevelUp = function () {
    return this._levelUpReward.length > 0;
};

userDao.prototype.levelUpReward = function () {
    return this._levelUpReward;
};

userDao.prototype.save = function (cb) {
    //console.error('data:' + JSON.stringify(this.data));
    //console.error('change:' + JSON.stringify(this.change));
    //let publicModel = new publicDao();
    if (!cb) {
        cb = function () {
        };
    }
    publicDao.init().table(tableName);
    if (!this.data.id) {
        for (let key in this.data) {
            publicDao.values(key, this.data[key]);
        }
        publicDao.insert(function (err, res) {
            if (err !== null) {
                cb({code: err.code, msg: err.message}, null);
            } else {
                cb(null, {res: res});
            }
        });
    } else {
        for (let i in this.change) {
            let key = this.change[i];
            //console.error(key + ':' + this.data[key]);
            publicDao.values(key, this.data[key]);
        }
        publicDao.wheres('id', '=', this.data.id);
        publicDao.update(function (err, res) {
            if (err !== null) {
                cb({code: err.code, msg: err.message}, null);
            } else {
                cb(null, {res: res});
            }
        });
    }
};